package com.example.weathernow;

import java.io.FileOutputStream;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class previewActivity extends Activity {

	private ImageView imgPreview;
	private Button btnSave;
	private String message;
	private Bitmap bitmap;
	private String localText, tempText, dateTimeText;
	private double lat, lon;
	GPSTracker gps;

	LocationManager locationManager;
	String provider;

	/*
	 * private final LocationListener mLocationListener = new LocationListener()
	 * {
	 * 
	 * @Override public void onLocationChanged(final Location location) { lat =
	 * location.getLatitude(); lon = location.getLongitude(); } };
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_preview);

		imgPreview = (ImageView) findViewById(R.id.imgPreview);
		// Get the message from the intent
		Intent intent = getIntent();
		message = intent.getStringExtra("imgPath");
		btnSave = (Button) findViewById(R.id.btnShared);
		btnSave.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// save picture
				saveImage();

			}
		});

		editImage();
	}

	private void saveImage() {
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(message);
			bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (Throwable ignore) {
			}
		}

		Intent intent = new Intent(previewActivity.this, MainActivity.class);
		intent.putExtra("from", "From_Activity_Preview");
		startActivity(intent);

	}

	/**
	 * Add Detail from Webservice
	 */
	public void editImage() {
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);
		/*
		 * locationManager = (LocationManager)
		 * getSystemService(Context.LOCATION_SERVICE); System.out.println("K1");
		 * 
		 * //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
		 * 0, 0, (android.location.LocationListener) this);
		 * System.out.println("K2");
		 * 
		 * Criteria criteria = new Criteria(); System.out.println("K3");
		 * 
		 * provider = locationManager.getBestProvider(criteria, false);
		 * System.out.println("K4");
		 * 
		 * Location location = locationManager.getLastKnownLocation(provider);
		 * if (location != null) {
		 * 
		 * System.out.println("Provider " + provider +
		 * " has been selected."+criteria); onLocationChanged(location); } else
		 * { System.out.println("location  = NULL"+criteria+provider); }
		 */
		gps = new GPSTracker(previewActivity.this);
		if (gps.canGetLocation()) {

			lat = gps.getLatitude();
			lon = gps.getLongitude();
		}
		System.out.println(lat + "," + lon);
		/*
		 * locationManager = (LocationManager)
		 * getSystemService(LOCATION_SERVICE);
		 * locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
		 * LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE, mLocationListener);
		 */
		myAsyncTask getLoctionDetail = new myAsyncTask(lat, lon);
		imgPreview.setVisibility(View.VISIBLE);
		// bimatp factory
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 5;
		bitmap = BitmapFactory.decodeFile(message, options);

		android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
		// set default bitmap config if none
		if (bitmapConfig == null) {
			bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
		}
		// resource bitmaps are imutable,
		// so we need to convert it to mutable one
		bitmap = bitmap.copy(bitmapConfig, true);

		Canvas canvas = new Canvas(bitmap);
		int ih = bitmap.getHeight();
		Calendar c = Calendar.getInstance();

		localText = getLoctionDetail.cityName + ","
				+ getLoctionDetail.CountryName;
		tempText = getLoctionDetail.temp + "c " + getLoctionDetail.conditonText;
		dateTimeText = c.get(Calendar.DATE) + "/" + c.get(Calendar.MONTH) + "/"
				+ c.get(Calendar.YEAR) + "  " + c.get(Calendar.HOUR) + ":"
				+ c.get(Calendar.MINUTE);

		Paint strokePaint = new Paint();
		strokePaint.setARGB(255, 0, 0, 0);
		strokePaint.setTextAlign(Paint.Align.LEFT);
		strokePaint.setTypeface(Typeface.DEFAULT_BOLD);
		strokePaint.setStyle(Paint.Style.STROKE);
		strokePaint.setStrokeWidth(2);

		Paint textPaint = new Paint();
		textPaint.setARGB(255, 255, 255, 255);
		textPaint.setTextAlign(Paint.Align.LEFT);
		textPaint.setTypeface(Typeface.DEFAULT_BOLD);

		canvas.drawBitmap(bitmap, 0, 0, null);

		textPaint.setTextSize(36);
		strokePaint.setTextSize(36);

		canvas.drawText(dateTimeText, 20, ih - 120, strokePaint);
		canvas.drawText(dateTimeText, 20, ih - 120, textPaint);

		textPaint.setTextSize(48);
		strokePaint.setTextSize(48);

		canvas.drawText(localText, 20, ih - 70, strokePaint);
		canvas.drawText(localText, 20, ih - 70, textPaint);

		textPaint.setTextSize(40);
		strokePaint.setTextSize(40);

		canvas.drawText(tempText, 20, ih - 20, strokePaint);
		canvas.drawText(tempText, 20, ih - 20, textPaint);

		imgPreview.setImageBitmap(bitmap);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.edit_image, menu);
		return true;
	}

}
